package pacman;

import pacman.objects.*;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * This class represents the map and contain the objects inside it
 * @author Carlos Massera Filho
 */
public class Maps {
  private Static[][] map;
  
  private int sizeX,
              sizeY;

  /**
   * Start the map in a fixed size
   * @param sizeX
   * @param sizeY
   */
  public Maps(int sizeX, int sizeY)
  {
    this.map = new Static[sizeX][sizeY];
    this.sizeX = sizeX;
    this.sizeY = sizeY;
  }

  /**
   * Load the map of the level from a file
   * @param filename The name of the file where the map will be loaded
   * @throws FileNotFoundException
   * @throws IOException
   */
  public void loadMap(String filename) throws FileNotFoundException, IOException
  {
    FileReader reader = new FileReader(new File(filename));
    BufferedReader readerBuffer = new BufferedReader(reader);

    String mapLine = null;
    for (int i = 0; i < getSizeY(); i++)
    {
      mapLine = readerBuffer.readLine();
      for(int j = 0; j < getSizeX(); j++)
      {
        switch(mapLine.charAt(j))
        {
          case '0':
            this.map[j][i] = new Dots(j, i);
          break;
          case '1':
            this.map[j][i] = new Wall(j, i);
          break;
          case '2':
            this.map[j][i] = new Food(j, i);
          break;
          case '3':
            this.map[j][i] = new Door(j, i);
          break;
        }
      }
    }

    readerBuffer.close();
    reader.close();
  }

  /**
   * Prints all objects inside the map
   * @param g Graphic interface
   */
  public void renderMap(Graphics g)
  {
    for (int i = 0; i < getSizeX(); i++)
      for (int j = 0; j < getSizeY(); j++)
      {
          map[i][j].render(g);
      }
  }

  /**
   * get the object in a specific map position
   * @param posX X coordinate
   * @param posY Y coordinate
   * @return The static object in (x,y)
   */
  public Static getMapContent(int posX, int posY)
  {
    return map[posX][posY];
  }

  /**
   * return the hole Static object matrix
   * @return
   */
  public Static[][] getMap()
  {
    return map;
  }

  /**
   * Inserts a object into map
   * @param object Object to be inserted
   */
  public void setMapContent(Static object)
  {
    map[(int)object.getPosX()][(int)object.getPosY()] = object;
  }

  /**
   * get the X size of the map
   * @return
   */
  public int getSizeX() {
    return sizeX;
  }

  /**
   * get the Y size of the map
   * @return
   */
  public int getSizeY() {
    return sizeY;
  }
}
